function MyPromise(excutor) {
  this.PromiseState = 'pending'
  this.PromiseResult = undefined
  this.callbacks = []
  const self = this // 下面两个函数没有调用它的对象，默认this指向为window

  function resolve_(data) {
    if (self.PromiseState !== 'pending') return
      // 1. 修改对象的状态
    self.PromiseState = 'fulfilled'
      // 2. 修改对象的结果值
    self.PromiseResult = data

    // 在这里调用then中的回调函数
    self.callbacks.forEach(callback => {
      // 异步链式调用
      callback.onResolved(self.PromiseResult)
    })
  }

  function reject_(data) {
    if (self.PromiseState !== 'pending') return
      // 1. 修改对象的状态
    self.PromiseState = 'rejected'
      // 2. 修改对象的结果值
    self.PromiseResult = data

    // 在这里调用then中的回调函数
    self.callbacks.forEach(callback => {
      callback.onRejected(self.PromiseResult)
    })
  }

  try {
    // 调用执行器函数
    excutor(resolve_, reject_)
  } catch (e) {
    // 修改promise状态为rejected
    reject_(e)
  }
}

MyPromise.prototype.then = function(onResolved, onRejected) {
  if (this.PromiseState === 'fulfilled') {
    try {
      // then方法的返回值
      let thenReturn = onResolved(this.PromiseResult)
      if (thenReturn instanceof MyPromise) {
        // 返回值为Mypromise对象
        return thenReturn
      } else {
        // 返回值为非MyPromise对象
        return new MyPromise((resolve, reject) => {
          resolve(thenReturn)
        })
      }
    } catch (e) {
      return new MyPromise((resolve, reject) => {
        reject(e)
      })
    }
  } else if (this.PromiseState === 'rejected') {
    onRejected(this.PromiseResult)
  } else if (this.PromiseState === 'pending') {

    this.callbacks.push({
      onResolved(PromiseResult) {
        let thenReturn = onResolved(PromiseResult)
        if (thenReturn instanceof MyPromise) {

        } else {
          return thenReturn = new MyPromise((resolve, reject) => {
            resolve(thenReturn)
          })
        }
      },
      onRejected(PromiseResult) {
        onRejected(PromiseResult)
      }
    })

  }

}

let m = new MyPromise((resolve, reject) => {
  setTimeout(function() {
    resolve('resolve')
  }, 1000)
})

m.then(value => {
  console.log(value + '000');
  return new MyPromise((resolve, reject) => {
    setTimeout(function() {
      resolve('resolveAgain')
    }, 1000)
  })
}, (reason) => {
  console.log(reason)
}).then(value => {
  console.log(value);
})